package com.ty.controller;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Consts.CursorMode;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.PullLogsRequest;
import com.aliyun.openservices.log.response.PullLogsResponse;

public class SampleLogHubProcessor   {
	
	public static void main(String[]args) throws LogException, InterruptedException {
		
		String accessId = "LTAI4G5B9NAWskR2fiZaSr77";    //阿里云访问密钥AccessKey ID。更多信息，请参见访问密钥。阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维。
		String accessKey = "6rC3aowKW7qqL44ATIgU8gFVJFOWiq";  //阿里云访问密钥AccessKey Secret。
		String host = "cn-beijing.log.aliyuncs.com";  //日志服务的域名。更多信息，请参见服务入口。此处以杭州为例，其它地域请根据实际情况填写。
		
		Client client = new Client(host, accessId, accessKey);

		String project = "klwxvideo";  //待创建的Project的名称。
		String logstore = "klwxvideo";     //待创建的Logstore的名称。
		int ttl_in_day = 3650;     //数据保存时间。如果配置为3650，表示永久保存。
		int shard_count = 10;   //Shard数量。
		int shardId = 0;  // 只读取0号shard的数据
	    String cursor = client.GetCursor(project, logstore, shardId, CursorMode.END).GetCursor();
	    System.out.println("cursor = " +cursor);
	    try {
	      while (true) {
	        PullLogsRequest request = new PullLogsRequest(project, logstore, shardId, 1000, cursor);
	        PullLogsResponse response = client.pullLogs(request);
	        System.out.println(response.getCount());
	        System.out.println("cursor = " + cursor + " next_cursor = " + response.getNextCursor());
	        if (cursor.equals(response.getNextCursor())) {
	            break;
	                }
	        cursor = response.getNextCursor();
	        Thread.sleep(200);
	      }
	    }catch(LogException e) {
	      System.out.println(e.GetRequestId() + e.GetErrorMessage());
	    }
	    
	}
}
